"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : longblob数据类型
Case Name   : gs_basebackup支持longblob类型的数据
Create At   : 2022.08.11
@zou_jialiang0501129488
Description :
    1、创建b库
    2、b库下查询dolphin插件
    3、建表并插入数据后执行basebackup进行备份后查询数据
    4、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

sh_primary = CommonSH('PrimaryDbUser')
Constant = Constant()


@unittest.skipIf(2 > sh_primary.get_node_num(), '非1主2备环境不执行')
class DatatypeTest01(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.user_node = Node('PrimaryDbUser')
        self.sh_sta1 = CommonSH('Standby1DbUser')
        self.sh_sta2 = CommonSH('Standby2DbUser')
        self.back_path = os.path.join(os.path.dirname(macro.DB_INSTANCE_PATH),
                                      'longblob_case0042_backup')
        self.log.info(sh_primary.get_db_cluster_status(param='detail'))

    def test_longblob(self):
        text = '-----step1:创建兼容b库数据库;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists longblob_case42;" \
                  f"create database longblob_case42 " \
                  f"dbcompatibility ='B' encoding ='UTF8';"
        self.log.info(sql_cmd)
        sql_res = sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(Constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)

        text = '-----step2:b库下查询dolphin插件;expect:自动加载成功,无需手动创建-----'
        self.log.info(text)
        sql_cmd = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd)
        sql_res = sh_primary.execut_db_sql(sql_cmd, dbname='longblob_case42')
        self.log.info(sql_res)
        self.assertIn('extname', sql_res, '查询失败')
        self.assertIn('dolphin', sql_res, '执行失败' + text)

        text = '-----step3:建表并插入数据后执行basebackup进行备份后查询数据;expect:成功-----'
        self.log.info(text)
        cmd = f"rm -rf {self.back_path};mkdir -p {self.back_path};" \
              f"chmod 700 {self.back_path};" \
              f"ls {os.path.dirname(macro.DB_INSTANCE_PATH)} | " \
              f"grep 'longblob_case0042_backup';"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.user_node, cmd)
        self.assertIn('longblob_case0042_backup', res, '文件夹创建失败')

        self.log.info('-----建表插数据-----')
        sql = f"drop table if exists t_longblob_042;" \
              f"create table t_longblob_042(field_name longblob);" \
              f"insert into t_longblob_042 values ('aaa'::blob);" \
              f"insert into t_longblob_042 values ('bbb'::blob);" \
              f"insert into t_longblob_042 values ('ccc'::blob);" \
              f"insert into t_longblob_042 values ('ddd'::blob);"
        self.log.info(sql)
        res = sh_primary.execut_db_sql(sql, dbname='longblob_case42')
        self.log.info(res)
        self.assertIn('CREATE TABLE', res, '建表失败')
        self.assertEqual(res.count('INSERT 0 1'), 4, '插入数据失败')

        self.log.info('-----执行basebackup-----')
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_basebackup -D {self.back_path} -Fp -X stream " \
              f"-p {self.user_node.db_port} -l gauss_042.bak " \
              f"-U {self.user_node.ssh_user} -w"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.user_node, cmd)
        self.assertIn('base backup successfully', res, '建表失败')

        sql = f"select * from t_longblob_042;"
        self.log.info(sql)
        check_msg = sh_primary.execut_db_sql(sql, dbname='longblob_case42')
        self.log.info(check_msg)

        self.log.info('-----stop原数据库-----')
        stop_res = sh_primary.stop_db_instance()
        self.assertIn('server stopped', stop_res, '建表失败')

        self.log.info('-----start备份数据库并查询状态-----')
        start_res = sh_primary.start_db_instance(dn_path=self.back_path)
        self.assertIn('server started', start_res, '建表失败')

        self.log.info('-----build备机-----')
        res1 = self.sh_sta1.build_standby(mode='')
        self.assertIn('server started', res1, '备1build失败')
        res2 = self.sh_sta2.build_standby(mode='')
        self.assertIn('server started', res2, '备2build失败')

        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_ctl query -D {self.back_path}"
        self.log.info(cmd)
        status = self.common.get_sh_result(self.user_node, cmd)
        self.assertTrue('Normal' or 'Degraded' in status, '数据库状态异常')

        res = sh_primary.execut_db_sql(sql, dbname='longblob_case42')
        self.log.info(res)
        self.assertEqual(check_msg, res, '文件数据至表失败')

    def tearDown(self):
        self.log.info('-----step4:清理环境; expect:成功-----')
        stop_res = sh_primary.stop_db_instance(dn_path=self.back_path)
        start_res = sh_primary.start_db_instance()
        res1 = self.sh_sta1.build_standby(mode='')
        res2 = self.sh_sta2.build_standby(mode='')
        status = sh_primary.get_db_cluster_status(param='detail')
        self.log.info(status)
        check_cmd = f'rm -rf {self.back_path};' \
                    f'if [ -d {self.back_path} ]; ' \
                    f'then echo "exists"; else echo "not exists"; fi'
        self.log.info(check_cmd)
        res = self.common.get_sh_result(self.user_node, check_cmd)
        sql_cmd = f"drop database if exists longblob_case42;"
        self.log.info(sql_cmd)
        sql_res = sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn('server stopped', stop_res, '数据库停止失败')
        self.assertIn('server started', start_res, '主机启动失败')
        self.assertIn('server started', res1, '备1build失败')
        self.assertIn('server started', res2, '备2build失败')
        self.assertTrue('Normal' or 'Degraded' in status, '数据库状态异常')
        self.assertEqual('not exists', res, '文件删除失败')
        self.assertIn('DROP DATABASE', sql_res, '数据库创建失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
